package com.example.jpademo;

import com.example.jpademo.exception.CustomNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserRepository repository;

    @GetMapping("/users")
    List<User> findAll(){
        return repository.findAll();
    }

    @GetMapping("/users/{id}")
    User findOne(@PathVariable String id){
        return repository.findById(id).orElseThrow(() -> new CustomNotFoundException(id));
    }

    @PostMapping("/users")
    User newUser(@Valid @RequestBody User user){
        return repository.save(user);
    }


}
